﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace NapazEngine.Framework.Graphics
{
    public class SpriteAnimation
    {
        //animation variables
        private int animationStep;                      //current step in the animation
        private int animationSteps;                     //total number of cells

        private double animationDuration;               //total duration of the animation
        private double animationStepDuration;           //duration for each step

        //texture cell sizes
        private int cellWidth;
        private int cellHeight;

        //time variables
        private double previousUpdateTime;
        private double newUpdateTime;


        /// <summary>
        /// Gets or sets the 
        /// </summary>
        public bool Active { get; private set; }


        /// <summary>
        /// Gets the rectangle for the current animation cell
        /// </summary>
        public Rectangle AnimationCellRectangle
        {
            get
            {
                return new Rectangle(animationStep * cellWidth, 0, cellWidth, cellHeight);
            }
        }


        /// <summary>
        /// The animation's texture
        /// </summary>
        public Texture2D Texture { get; private set; }


        /// <summary>
        /// SpriteAnimation constructor
        /// </summary>
        /// <param name="texture">Texture for the animation</param>
        /// <param name="cellWidth">Width of an animation cell</param>
        /// <param name="cellHeight">Height of an animation cell</param>
        /// <param name="cellCount">Number of cells in the animation</param>
        /// <param name="animationDuration">total length of the animation</param>
        public SpriteAnimation(Texture2D texture, int cellWidth, int cellHeight, int cellCount, double animationDuration)
        {
            //status
            Active = false;

            //animation variables
            this.animationStep = 0;
            this.animationSteps = cellCount;
            this.animationDuration = animationDuration;
            this.animationStepDuration = animationDuration / cellCount;

            this.cellWidth = cellWidth;
            this.cellHeight = cellHeight;
            this.Texture = texture;
        }


        /// <summary>
        /// Updates the animation
        /// </summary>
        /// <param name="gameTime"></param>
        public void Update(GameTime gameTime)
        {
            if (Active)
            {
                //update our time difference
                newUpdateTime = gameTime.TotalGameTime.TotalMilliseconds;

                if (newUpdateTime - previousUpdateTime > animationStepDuration)
                {
                    //zero out time difference
                    previousUpdateTime = newUpdateTime;

                    animationStep++;
                    
                    if (animationStep > animationSteps)
                    {
                        animationStep = 0;
                        Active = false;
                    }
                }
            }
        }
        

        /// <summary>
        /// Sets the animation to play
        /// </summary>
        public void Play()
        {
            animationStep = 0;
            Active = true;
        }


        /// <summary>
        /// Sets the animation to play
        /// </summary>
        /// /// <param name="cellStart">Cell frame to start animation at</param>
        public void Play(int cellStart)
        {
            animationStep = cellStart;
            Active = true;
        }


        /// <summary>
        /// Pauses the animation at the current frame
        /// </summary>
        public void Pause()
        {
            Active = false;
        }


        /// <summary>
        /// Resumes the animation
        /// </summary>
        public void Resume()
        {
            Active = true;
        }


        /// <summary>
        /// Sets the speed of the animation
        /// </summary>
        /// <param name="duration"></param>
        public void SetAnimationSpeed(double duration)
        {
            this.animationDuration = duration;
            this.animationStepDuration = animationDuration / animationSteps;
        }
    }
}